#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>

int main(int argc, char* argv[])
{
    pid_t id = fork();
    if(id == 0)
    {    
        sleep(1);
        printf("我是一个子进程: %d\n", getpid());
        sleep(1);
        
        //execl("/usr/bin/ls", "ls", "-l", "-a", NULL);
        
         //char* const myargv[] = {
         //  (char*)"ls",
         //  (char*)"-a",
         //  (char*)"-l",
         //  NULL
         //};
        //char *const myargv[] = {(char*)"pwd",NULL};
        //execv("/usr/bin/pwd", myargv);
        
        //char **myargv = &argv[1];//命令行参数表
        //execv(myargv[0], myargv); 
        
        //execlp("ls", "ls", "-l", "-a", NULL);
        //execvp(myargv[0], myargv);
        
        //execl("./mycmd", "mycmd", NULL);
        //execl("/usr/bin/python3", "python3", "mypy.py", NULL);
        //execl("/usr/bin/bash", "bash", "test.sh", NULL);
       
        char* const myargv[] = {"mycmd", NULL};
       //char* const myenv[] = {"hell123", "321", NULL};
        extern char** environ;//系统环境变量
        execvpe("./mycmd", myargv, environ);

        printf("程序替换失败!\n");
        exit(1);
    }
    //父进程
    pid_t pid = waitpid(id, NULL, 0);
    if(pid > 0)
    {
        printf("等待成功!\n");
    }
    return 0;
}
